package com.tfd.blog.repository;

import com.tfd.blog.domain.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author TangFD@HF 2019/5/8
 */
@Repository
public interface CategoryRepository extends JpaRepository<Category, String> {

    /**
     * 按照id批量删除
     *
     * @param ids id的集合
     */
    @Transactional(rollbackFor = Exception.class)
    void deleteCategoryByIdIn(List<String> ids);

    /**
     * 通过类别名称分页查询
     *
     * @param categoryName 类别名称
     * @param pageable     分页对象
     * @return 分页
     */
    @Query(value = "SELECT * FROM t_blog_category act WHERE act.category_name LIKE ?1 ORDER BY updated_at DESC",
            countQuery = "SELECT count(*) FROM t_blog_category WHERE category_name LIKE ?1",
            nativeQuery = true)
    Page<Category> pageByName(String categoryName, Pageable pageable);

    /**
     * 根据类别Id列表获取类别实体
     *
     * @param categoryIds 类别Id列表
     * @return 类别实体列表
     */
    List<Category> findByIdIn(Collection<String> categoryIds);

    @Query(value = "SELECT id FROM t_blog_category", nativeQuery=true)
    List<String> findAllIds();
}
